โ– humdrum codex / soft
766 B raw
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { withErrors, ok, apiError, parseJson } from "@/lib/api";
import { requireUser } from "@/lib/auth";
import { UpdateTodo } from "@/lib/schemas/todo";
import { updateTodo, deleteTodo } from "@/lib/store";

// PATCH /api/todos/:id โ€” toggle done, edit title, set due.
export const PATCH = withErrors(async (req, { params }) => {
  await requireUser(req);
  const { id } = await params;
  const patch = await parseJson(req, UpdateTodo);
  const todo = updateTodo(id, patch);
  if (!todo) return apiError("not_found", "Todo not found", 404);
  return ok({ todo });
});

// DELETE /api/todos/:id
export const DELETE = withErrors(async (req, { params }) => {
  await requireUser(req);
  const { id } = await params;
  deleteTodo(id);
  return ok({ ok: true });
});